package com.cskaoyan.mall.service;

import com.cskaoyan.mall.dao.UserDao;
import com.cskaoyan.mall.dao.UserDaoImpl;
import com.cskaoyan.mall.model.UpdateUserPwd;
import com.cskaoyan.mall.model.admin.AdminLoginBO;
import com.cskaoyan.mall.model.user.User;
import com.cskaoyan.mall.model.user.UserForUpdate;
import com.cskaoyan.mall.model.user.UserVOAndUserSignUpBO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: jyf2000jyf
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    @Override
    public List<UserVOAndUserSignUpBO> getAllUser() {
        List<UserVOAndUserSignUpBO> userVOAndUserSignUpBOList = new ArrayList<>();
        List<User> userList = userDao.getAllUser();
        for (User user : userList) {
            user.setPassword(null);
            userVOAndUserSignUpBOList.add(new UserVOAndUserSignUpBO(user));
        }
        return userVOAndUserSignUpBOList;
    }

    @Override
    public boolean deleteUser(int id) {
        return userDao.deleteUser(id);
    }

    @Override
    public List<UserVOAndUserSignUpBO> getSearchUser(String word) {
        // if(StringUtils.isEmpty(word)){
        //     return getAllUser();
        // }
        List<UserVOAndUserSignUpBO> userVOAndUserSignUpBOList = new ArrayList<>();
        List<User> userList = userDao.getSearchUser(word);
        for (User user : userList) {
            user.setPassword(null);
            userVOAndUserSignUpBOList.add(new UserVOAndUserSignUpBO(user));
        }
        return userVOAndUserSignUpBOList;
    }

    @Override
    public Map<String,Object> login(AdminLoginBO adminLoginBO) {
        Map<String,Object> map = new HashMap<>();
        Integer code = userDao.login(adminLoginBO);
        String nickname = userDao.getUserNickname(adminLoginBO);
        map.put("code", code);
        map.put("nickname", nickname);
        return map;
    }

    @Override
    public boolean addUser(UserVOAndUserSignUpBO userVOAndUserSignUpBO) {
        return userDao.addUser(new User(userVOAndUserSignUpBO));
    }

    @Override
    public UserForUpdate getUserByUserName(String token) {
        return userDao.getUserByUserName(token);
    }

    @Override
    public boolean updateUserData(UserForUpdate userForUpdate) {
        return userDao.updateUserData(userForUpdate);
    }

    @Override
    public String updatePwd(UpdateUserPwd pwd) {
        if(pwd.getNewPwd().isEmpty() || pwd.getNewPwd() == null || pwd.getConfirmPwd().isEmpty() || pwd.getConfirmPwd() == null){
            return null;
        }
        if(!pwd.getNewPwd().equals(pwd.getConfirmPwd())){
            // 不会造成冲突，不合理的密码
            return "确认密码和新密码不一致";
        }
        return userDao.updatePwd(pwd);
    }
}
